export function test() {
    let date = new Date();
    console.log('date:', date);
    console.log('getYear', date.getYear());
    console.log('闰年：', isLeapYear(date));
    console.log('yyyy-mm-dd', date.Format('yyyy-MM-dd'));
}

export function isLeapYear(year) {
    year = Number(year);
    if (!year) {
        return false;
    }
    return (0 == year % 4 && ((year % 100 != 0) || (year % 400 == 0)));
}

export function dateToStr(date, format) {
    return date.Format(format);
}

//获取当前时间（yyyy-mm-dd hh:mm:ss）
export function formatDateTime() {
    var y = new Date().getFullYear();
    var m = new Date().getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    var d = new Date().getDate();
    d = d < 10 ? "0" + d : d;
    var h = new Date().getHours();
    h = h < 10 ? "0" + h : h;
    var minute = new Date().getMinutes();
    minute = minute < 10 ? "0" + minute : minute;
    var second = new Date().getSeconds();
    second = second < 10 ? "0" + second : second;
    return (
        y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second
    );
}

Date.prototype.Format = function (formatStr) {
    var str = formatStr;
    var Week = ['日', '一', '二', '三', '四', '五', '六'];

    str = str.replace(/yyyy|YYYY/, this.getFullYear());
    str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() % 100));

    var month = this.getMonth() + 1;
    str = str.replace(/MM/, month > 9 ? month.toString() : '0' + month);
    str = str.replace(/M/g, month.toString());


    str = str.replace(/w|W/g, Week[this.getDay()]);

    str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate());
    str = str.replace(/d|D/g, this.getDate());

    str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours());
    str = str.replace(/h|H/g, this.getHours());
    str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes());
    str = str.replace(/m/g, this.getMinutes());

    str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds());
    str = str.replace(/s|S/g, this.getSeconds());

    return str;
}

export function getMonthDays(year, month) {
    var d = new Date(year, month, 0);
    return d.getDate();
}
/**
 * ## 生日 转 年龄
 * @param {Date|string} inBirth date 或 string
 * @param {string} format 输出格式, 现支持：['直接返回'=>'x岁x月']
 */
export function birthToAge(inBirth, format) {
    let replyMsg = ({ res, age, unit, age2, unit2 }) => {
        format = format || '直接返回'
        let outData = null
        switch (format) {
            case '直接返回':
                if (res != 'success') {
                    outData = res
                    break
                }
                outData = age + unit
                if (age2 != null) {
                    outData += age2 + unit2
                }
                break

            default:
                outData = '格式错误'
                break;
        }
        return outData
    }
    // --- 输入转换
    let birth = null;
    switch (Object.prototype.toString.call(inBirth)) {
        case '[object Date]':
            birth = inBirth
            break;
        case '[object String]':
            birth = new Date(inBirth)
            break;

        default:
            return replyMsg({ res: "时间输入错误，仅支持 string | Date 类型" })
            break;
    }
    if (!birth || birth == 'Invalid Date') {
        return replyMsg({ res: "输入了非法的时间" })
    }

    // --- 数据准备
    let birthYear = birth.getFullYear()
    let birthMonth = birth.getMonth()
    let birthDay = birth.getDate()
    let today = new Date()
    let nowYear = today.getFullYear()
    let nowMonth = today.getMonth()
    let nowDay = today.getDate()
    // --- 错误判断
    if (today.getTime() < birth.getTime()) {
        return replyMsg({ res: '时间错误' })
    }

    // --- 绝对时间差
    let absDeltaYear = nowYear - birthYear
    let absDeltaMonth = nowMonth - birthMonth
    if (absDeltaMonth >= 0) {
        absDeltaMonth = absDeltaYear * 12 + absDeltaMonth
    } else {
        absDeltaYear--
        absDeltaMonth = absDeltaYear * 12 + (12 + absDeltaMonth)
    }
    if (nowDay < birthDay) {
        absDeltaMonth--
        absDeltaYear = Math.floor(absDeltaMonth / 12)
    }
    let absDeltaDay = (today.getTime() - birth.getTime()) / 86400000

    // --- 输出
    // 小于3天 : x小时
    if (absDeltaDay <= 3) {
        let hours = (today.getTime() - birth.getTime()) / 3600000
        hours = Math.round(hours)
        return replyMsg({
            res: 'success',
            age: hours,
            unit: '时'
        })
    }
    // 小于1岁：x月x天
    else if (absDeltaMonth < 12) {
        let deltaDay = nowDay - birthDay
        if (deltaDay < 0) {
            let lastMonthDays = getMonthDays(nowMonth > 0 ? nowYear : nowYear - 1, nowMonth > 0 ? nowMonth - 1 : 11)
            deltaDay = lastMonthDays - birthDay + nowDay;
        }
        if (absDeltaMonth <= 0) {
            return replyMsg({
                res: 'success',
                age: deltaDay,
                unit: '天',
            })
        } else {
            return replyMsg({
                res: 'success',
                age: absDeltaMonth,
                unit: '月',
                age2: deltaDay || null,
                unit2: '天'
            })
        }
    }
    // 小于5岁：x岁x月
    if (absDeltaYear < 5) {
        return replyMsg({
            res: 'success',
            age: absDeltaYear,
            unit: '岁',
            age2: (absDeltaMonth % 12) || null,
            unit2: '月'
        })
    }
    // x岁
    else {
        return replyMsg({
            res: 'success',
            age: absDeltaYear,
            unit: '岁'
        })
    }
}


export function getWeekDataList(data) {
	//根据日期获取本周周一~周日的年-月-日
	var weekList = [];
	var date = new Date(data);
	//判断本日期是否为周日，获取本周一日期
	if(date.getDay()=="0"){
		date.setDate(date.getDate() -6);
	}else {
		date.setDate(date.getDate() - date.getDay() + 1);
	}
	var myDate=date.getDate();
	var myMonth=date.getMonth() + 1;
	if(date.getDate()<10){
		myDate= '0'+ myDate;
	}
	if(date.getMonth() + 1<10){
		myMonth='0'+myMonth;
	}
	weekList.push(date.getFullYear() + "-" + myMonth+ "-" + myDate);
	// 获取周二以后日期
	for(var i=0;i<6;i++) {
		date.setDate(date.getDate() + 1);
		myDate=date.getDate();
		myMonth=date.getMonth() + 1;
		if(date.getDate()<10){
			myDate= '0'+ myDate;
		}
		if(date.getMonth() + 1<10){
			myMonth='0'+myMonth;
		}
		weekList.push(date.getFullYear() + "-" + myMonth+ "-" +myDate);
	}
	console.log(weekList);
	return weekList
}
